<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
	#schidView{ width:170px;height: 32px;padding-left:12px;border:none; background:#fff !important;  }
</style>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox-content">
					<input id="pageType" th:value="${pageType}"  type="hidden">

					<form class="form-horizontal m-t" id="signupForm">
						<input id="id" name="id" th:value="${user.id}" type="hidden">
						<div class="form-group">
							<label class="col-sm-3 control-label"><em class="text-danger">*&nbsp;</em>单位:</label>
							<div class="col-sm-8">
								<select  class="form-control" id="schidSelect" name="unitid" style="width:196px;">
									<option  th:selected="${user.unitid eq c.key}" th:each="c:${schMap}"  th:value="${c.key}" th:text="${c.value}" ></option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label"><em class="text-danger">*&nbsp;</em>姓名：</label>
							<div class="col-sm-8">
								<input id="name" name="name" class="form-control" type="text" th:value="${user.name}">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label"><em class="text-danger">*&nbsp;</em>用户名：</label>
							<div class="col-sm-8">
								<input id="username" name="username" class="form-control" type="text" th:value="${user.username}" >
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label">身份证号：</label>
							<div class="col-sm-8">
								<input id="idcard" name="idcard" class="form-control" type="text" th:value="${user.idcard}" >
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label">E-mail：</label>
							<div class="col-sm-8">
								<input id="email" name="email" class="form-control" type="email" th:value="${user.email}">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label">手机号：</label>
							<div class="col-sm-8">
								<input id="mobile" name="mobile" class="form-control" type="text"  th:value="${user.mobile}">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label"><em class="text-danger">*&nbsp;</em>用户类型:</label>
							<div class="col-sm-8">
								<select  class="form-control" id="usertype" name="usertype">
									<option  th:selected="${user.usertype eq c.key}" th:each="c:${application.jyrc_usertype_}"  th:value="${c.key}" th:text="${c.value}" ></option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label"><em class="text-danger">*&nbsp;</em>状态:</label>
							<div class="col-sm-3">
								<select  class="form-control" id="status" name="status">
									<option  th:selected="${user.status eq c.key}" th:each="c:${application.jyrc_userstatus_}"  th:value="${c.key}" th:text="${c.value}" ></option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-8 col-sm-offset-5">
								<button type="submit" id="btnSave" class="btn btn-primary">提交</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
		<div>
			<script type="text/javascript">
                var sjpriv = false;
                var qjpriv = false;
                var xjpriv = false;
			</script>
		</div>
		<div shiro:hasPermission="sys:sjpriv">
			<script type="text/javascript">
                sjpriv = true;
			</script>
		</div>
		<div shiro:hasPermission="sys:qjpriv">
			<script type="text/javascript">
                qjpriv = true;
			</script>
		</div>
		<div shiro:hasPermission="sys:xjpriv">
			<script type="text/javascript">
                xjpriv = true;
			</script>
		</div>
	</div>
	<div th:include="include::footer"></div>
	<script type="text/javascript">
        $().ready(function() {

			//自动完成框配置
            var config = {};
            config.suggestWidth = 190;
            config.el = "schid";
            config.title = "名称/拼音首字母";
            if(sjpriv){
                config.url = "/sys/unitBasicinfo/getSchoolAutoComplete.do";
            }else if(qjpriv){
                config.url = "/sys/unitBasicinfo/getSchoolAutoComplete.do?curssqx=true";
            }
            //自动完成框配置


            var pageType = $('#pageType').val();
            if ('save' == pageType) {//新增页面
                validateRuleAd();
                if(sjpriv || qjpriv) {//自动完成相关
                    $('#schidSelect').selectbox(config);
                }
            } else if ('update' == pageType) {//修改页面
                validateRuleUp();
                if(sjpriv || qjpriv) {//自动完成相关
                    $('#schidSelect').selectbox(config);
                    $('#schid').val($('#schidSelect').val());//id
                    $('#schidView').val($("#schidSelect").find("option:selected").text());//名称
                    $('#schidFlag').val('0');
                }

                $('#usertype').attr({"disabled": "disabled"});
                $('#username').attr({"disabled": "disabled"});
            } else if ('look' == pageType) {//查看页面

                if(sjpriv || qjpriv) {//自动完成相关
                    $('#schidSelect').selectbox(config);
                    $('#schid').val($('#schidSelect').val());//id
                    $('#schidView').val($("#schidSelect").find("option:selected").text());//名称
                    $('#schidFlag').val('0');
                }
                Util.disableInput();//页面不可修改

            }else {
                parent.layer.msg("页面参数错误");
            }
        })
        $.validator.setDefaults({
            submitHandler : function() {
                update();
            }
        });
        function update() {
            $.ajax({
                cache : true,
                type : "POST",
                url : "/sys/user/"+$('#pageType').val(),
                data : $('#signupForm').serialize(),// 你的formid
                async : false,
                error : function(request) {
                    alert("Connection error");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg(data.msg);
                        $(window.parent.document).find('.J_iframe').each(function () {
                            if ($(this).data('id') == "/sys/user") {//common/sysDict为打开的frame id
                                this.contentWindow.reLoad();
                                return false;
                            }
                        });
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);
                    } else {
                        parent.layer.msg(data.msg);
                    }
                }
            });

        }

        function validateRuleUp() {
            var icon = "<i class='fa fa-times-circle'></i> ";
            $("#signupForm").validate({
                rules : {
                    unitid : {
                        required : true
                    },
                    name : {
                        required : true
                    },
                    username : {
                        required : true,
                        minlength : 2
                    },
                    email : {
                        email : true
                    }
                },
                messages : {
                    unitid : {
                        required : icon + "请选择单位"
                    },
                    name : {
                        required : icon + "请输入姓名"
                    },
                    username : {
                        required : icon + "请输入您的用户名",
                        minlength : icon + "用户名必须两个字符以上"
                    }
                }
            })
        }
            function validateRuleAd() {
                var icon = "<i class='fa fa-times-circle'></i> ";
                $("#signupForm").validate({
                    rules : {
                        unitid : {
                            required : true
                        },
                        name : {
                            required : true
                        },
                        username : {
                            required : true,
                            minlength : 2,
                            remote : {
                                url : "/sys/user/exit", // 后台处理程序
                                type : "post", // 数据发送方式
                                dataType : "json", // 接受数据格式
                                data : { // 要传递的数据
                                    username : function() {
                                        return $("#username").val();
                                    }
                                }
                            }
                        },
                        email : {
                            email : true
                        }
                    },
                    messages : {
                        unitid : {
                            required : icon + "请选择单位"
                        },
                        name : {
                            required : icon + "请输入姓名"
                        },
                        username : {
                            required : icon + "请输入您的用户名",
                            minlength : icon + "用户名必须两个字符以上",
                            remote : icon + "用户名已经存在"
                        }
                    }
                })
            }
	</script>
</body>
</html>
